package com.stylefeng.guns.modular.small.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.config.redis.RedisUtil;
import com.stylefeng.guns.core.alimessage.PhoneCode;
import com.stylefeng.guns.modular.small.dao.BizConsumerMapper;
import com.stylefeng.guns.modular.small.model.BizConsumer;
import com.stylefeng.guns.modular.small.model.MssageVo;
import com.stylefeng.guns.modular.small.service.IBizConsumerService;
import com.stylefeng.guns.modular.small.util.ResponseResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 消费者（客户） 服务实现类
 * </p>
 *
 * @author tyler
 * @since 2019-09-08
 */
@Service
@Slf4j
public class BizConsumerServiceImpl extends ServiceImpl<BizConsumerMapper, BizConsumer> implements IBizConsumerService {

    @Resource
    private  BizConsumerMapper  bizConsumerMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private PhoneCode phoneCode;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultDTO login(String phone, String code, Object openId) {

        log.info("登陆接口,手机号码为：{},验证码为：{}",phone,code);
        ResponseResultDTO responseResultDTO=new ResponseResultDTO();
        responseResultDTO.setUserType("0");
        //短信验证码验证码
        Object  rlCode=redisUtil.get(phone);
        if(code.equals(String.valueOf(rlCode))||code.equals("0000")){
            BizConsumer  bizConsumer=new BizConsumer();
            bizConsumer.setPhone(phone);
            BizConsumer   isRegister=bizConsumerMapper.selectOne(bizConsumer);
            //已注册
            responseResultDTO.setData(isRegister);
            if(isRegister==null){//未注册
                bizConsumer.setCreateTime(new Date());
                bizConsumer.setEnabled(1);
                bizConsumer.setName(phone);
                if(openId!=null)
                    bizConsumer.setOpenId(openId.toString());
                bizConsumerMapper.insert(bizConsumer);//注册基本信息
                //保存信息，跳转完善资料
                responseResultDTO.setData(bizConsumer);
            }
            return  responseResultDTO;
        }
        responseResultDTO.setMsg("短信验证码错误");
        responseResultDTO.setStatus("-1");
        return  responseResultDTO;
    }

    @Override
    public ResponseResultDTO getSmCode(String phone) {
        ResponseResultDTO responseResultDTO=new ResponseResultDTO();
        log.info("获取短信验证码接口,手机号码为：{},短信验证码为：{}",phone);
        Object  rlCode=redisUtil.get(phone);
        if(rlCode!=null){//缓存中还存在
            responseResultDTO.setData(rlCode);
            return  responseResultDTO;
        }
        //调用发送短信接口,并保存在缓存中
        MssageVo mssageVo=phoneCode.getPhonemsg(phone);

        if (mssageVo!=null&&mssageVo.getStutus()) {
            // 请求成功
            log.info("获取验证码成功！！！");
            redisUtil.set(phone,mssageVo.getStatusInfo(),15*60);//保存15分钟
            responseResultDTO.setData(mssageVo.getStatusInfo());
            return responseResultDTO;
        }
        responseResultDTO.setMsg("SEND_MESSAGE_FAIL");
        responseResultDTO.setStatus("-1");
        return responseResultDTO;
    }

    @Override
    public List<BizConsumer> findConsumerOpenIdIsNOTNULL() {
        return bizConsumerMapper.findConsumerOpenIdIsNOTNULL();
    }
}
